package echat.future.cn.echat.my.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import echat.future.cn.echat.R;
import echat.future.cn.echat.my.utils.ToastUtils;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;


/**
 * 项目名称:etc_wallet
 * 类描述 基础的Activity
 *
 * @author ch
 * @email 869360026@qq.com
 * 创建时间:2018/6/22 14:30
 */
public abstract class BaseActivity<V extends ViewDataBinding> extends AppCompatActivity {
    protected V bind;
    /**
     * baseActivity 根目录
     */
    private LinearLayout root;

    /**
     * 获得布局文件的layoutId
     *
     * @return
     */
    public abstract int layoutId();

    /**
     * 获取导航栏
     *
     * @return
     */
    public abstract IBaseNavagation navagation();


    public abstract void initView();


    /**
     * Activity预处理
     */
    public void preOnCreate(){

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preOnCreate();
        setContentView(R.layout.base_activity);
        this.root = (LinearLayout) findViewById(R.id.root);
        if (null != navagation()) {
            this.root.addView(navagation().getNavagation());
        }
        View rootView = bindViews(LayoutInflater.from(this));
        if(rootView == null) {
            rootView = LayoutInflater.from(this).inflate(layoutId(), null);
        }
        rootView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this.root.addView(rootView);
        initView();
    }

    protected  View bindViews(LayoutInflater inflater){
        bind = DataBindingUtil.inflate(inflater,layoutId(),null,false);
        return bind.getRoot();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public void toast(String msg) {
        ToastUtils.toastMsg(this, msg);
    }


    public static void openActivity(Context context,Class cls){
        context.startActivity(new Intent(context,cls));
    }

    public static void openActivity(Activity context, Class cls, int requestCode){
        context.startActivityForResult(new Intent(context,cls),requestCode);
    }

    public void drawbackgroud(float alpha){
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = alpha;
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.getWindow().setAttributes(lp);
    }


    public void initData(){}
}
